<template>
  <div>
    <Card :bordered="false" dis-hover>
      <Input search v-model="musicName" clearable @on-search="search()" placeholder="搜索音乐、歌手、专辑"/>
      <Divider/>
      <Table :columns="musicColumn" :data="musicData" :show-header="false"></Table>
    </Card>
  </div>
</template>

<script>
import qs from "qs";
const fs = require("fs");

import Enumerable from "linq";
import { mapActions, mapGetters } from "vuex";

import searchTableColumns from "./search-table-columns";
import musicApi from "@suen/music-api";

export default {
  name: "Search",
  data() {
    return {
      musicName: "",
      sourceUrl:
        "http://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.center&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=100&jsonpCallback=searchCallbacksong2020&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0",
      analysisUrl: "https://www.sinsyth.com/lxapi/ssk/yqq.x",
      musicColumn: searchTableColumns(this),
      musicData: []
    };
  },
  created() {
    // musicColumn = searchTableColumns('');
  },
  computed: {
    ...mapGetters(["playlist"])
  },
  methods: {
    search() {
      if (this.musicName == "") this.musicName = "周杰伦";
      musicApi
        .searchSong(this.musicName)
        .then(result => {
          if (result.status == false) {
            this.$Message.error("系统错误，请稍后再试");
          } else {
            this.musicData = result.data.qq.songs;
          }
        })
        .catch(() => {
          this.$Message.error("系统错误，请稍后再试");
        });
    },

    download() {
      //     // this.$http({
      //     //   url: rendata.Song,
      //     //   responseType: "arraybuffer"
      //     // }).then(response => {
      //     //   console.log("success", response);
      //     //   var reader = new FileReader();
      //     //   // reader.read(response.data);
      //     //   // console.log(reader);
      //     //   var buffer = new Buffer(response.data);
      //     //   //temp文件夹应已存在
      //     //   fs.writeFile(
      //     //     `E:/${originMusic.name}.flac`,
      //     //     buffer, {},
      //     //     (err, res) => {
      //     //       if (err) {
      //     //         console.error(err);
      //     //         return;
      //     //       }
      //     //       console.log("video saved");
      //     //     }
      //     //   );
      //     // });
    },
    play(index, br = 320000) {
      // music-api
      var originMusic = this.musicData[index];
      originMusic.br = br;

      var playlist = Enumerable.from(this.playlist);
      if (playlist.any(p => p.id == originMusic.id)) {
        var index = playlist.indexOf(p => p.id == originMusic.id);
        this.playlist[index].br = br;
        this.setCurrentIndex(-1);
        this.setCurrentIndex(index);
      } else {
        this.playlist.push(originMusic);
        // this.setPlaylist();
        this.setCurrentIndex(this.playlist.length - 1);
      }
    },
    ...mapActions(["setPlaylist", "setCurrentIndex"])
  }
};
</script>